<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body onload="init()">
		请选择网页背景颜色：<select onchange="fun()"></select>
	</body>
	<script type="text/javascript">
		let colors = [];
		function init(){
			let xhr = new XMLHttpRequest();
			xhr.open("GET","color.xml",false);
			xhr.send();
			let data = xhr.responseXML;
			colors = data.querySelectorAll("color");
			let s = document.querySelector("select");
			for (let i = 0; i < colors.length; i++) {
				let color = colors[i].querySelector("name").innerHTML;
				s.options[i] = new Option(color);//下拉选项第i个选项
			}
			let cv = colors[0].querySelector("value").innerHTML;
			document.body.setAttribute("style","background-color:"+cv+";");	//更改样式setAttribute以及填入双引号加两个加号再加参数
		}
		let index = 0;
		function fun(){
			index = document.querySelector("select").selectedIndex;//展示所选项的需求
			let cv = colors[index].querySelector("value").innerHTML;
			document.body.setAttribute("style","background-color:"+cv+";");	
		}
	</script>
</html>

